package com.meteor.downloader.videodownloader.activity.downloader

import android.annotation.SuppressLint
import android.content.Intent
import android.view.View
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.meteor.downloader.videodownloader.R
import com.meteor.downloader.videodownloader.activity.MdmBindingActivity
import com.meteor.downloader.videodownloader.adapter.AdapterDownloadingItem
import com.meteor.downloader.videodownloader.admdm.MdmDealAdUtils
import com.meteor.downloader.videodownloader.databinding.ActivityMdmDownloadingBinding
import com.meteor.downloader.videodownloader.dismissGoMdm
import com.meteor.downloader.videodownloader.downloading.MdmDownloadingService
import com.meteor.downloader.videodownloader.downloading.VideoDownloadingListener
import com.meteor.downloader.videodownloader.mdmdata.DataDownloadingMdm
import com.meteor.downloader.videodownloader.utils.MdmTotalUtils
import com.meteor.downloader.videodownloader.utils.dialog.DialogInitMdmLoading
import com.meteor.downloader.videodownloader.utils.value.ParamsMdmUtils.mDownloadingMdmList
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class MdmDownloadingActivity : MdmBindingActivity<ActivityMdmDownloadingBinding>() {
    private lateinit var mAdapterDownloadingItem: AdapterDownloadingItem
    private val mListDoData = mutableListOf<DataDownloadingMdm>()

    override fun doBackPressed() {
        showBookAdMdm("mdmBack") {
            super.doBackPressed()
        }
    }

    override fun beginViewAndDoMdm() {
        mStarNativeValue = true
        mNativeBookLayout = mMdmBinding.flMdmNativeView
        mMdmBinding.topSetMdm.tvTitleAll.text = getString(R.string.mdm_downloading)
        initRecycler()
        showData()
        lifecycleScope.launch(Dispatchers.Main) {
            delay(288)
            MdmDealAdUtils.loadBookIntersMdm()
        }
    }

    private fun initRecycler() {
        mAdapterDownloadingItem = AdapterDownloadingItem(this, mListDoData, {
            val data = mListDoData[it]
            data.mCancelMdm = true
            data.mTaskMdm?.cancel()
            mListDoData.removeAt(it)
            mDownloadingMdmList.remove(data)
            mAdapterDownloadingItem.notifyItemRemoved(it)
            if (mListDoData.isEmpty()) {
                mMdmBinding.clNoDataList.visibility = View.VISIBLE
                mMdmBinding.rvMdmList.visibility = View.GONE
            }
        }, {
            val data = mListDoData[it]
            data.mVideoPauseMdm = !data.mVideoPauseMdm
            if (data.mVideoPauseMdm) {
                data.mQueueVideoMdm?.pause()
            } else {
                data.mQueueVideoMdm?.resume()
            }
            mAdapterDownloadingItem.notifyItemChanged(it, true)
        }) {
            showBookAdMdm("", true) {
                val data = mListDoData[it]
                MdmTotalUtils.playVideoMdm(this, data.mCacheFileMdm)
            }
        }
        mMdmBinding.rvMdmList.layoutManager = LinearLayoutManager(this)
        mMdmBinding.rvMdmList.adapter = mAdapterDownloadingItem
    }

    @SuppressLint("NotifyDataSetChanged")
    private fun showData() {
        for (i in mDownloadingMdmList.size - 1 downTo 0) {
            val data = mDownloadingMdmList[i]
            if (data.mProgressMdm == 100 || data.mCancelMdm) {
                mDownloadingMdmList.removeAt(i)
            }
        }
        startService(Intent(this, MdmDownloadingService::class.java))
        mListDoData.clear()
        lifecycleScope.launch(Dispatchers.Main) {
            mListDoData.addAll(mDownloadingMdmList)
            mListDoData.reverse()
            if (mListDoData.isEmpty()) {
                mMdmBinding.rvMdmList.visibility = View.GONE
                mMdmBinding.clNoDataList.visibility = View.VISIBLE
            } else {
                mMdmBinding.rvMdmList.visibility = View.VISIBLE
                mMdmBinding.clNoDataList.visibility = View.GONE
            }
            mAdapterDownloadingItem.notifyDataSetChanged()
            for (index in mListDoData.indices) {
                val data = mListDoData[index]
                launch {
                    data.mVideoDownloadingListener = object : VideoDownloadingListener {
                        override fun progress() {
                            mAdapterDownloadingItem.notifyItemChanged(index, true)
                        }

                        override fun complete() {
                            mAdapterDownloadingItem.notifyItemChanged(index, true)
                        }
                    }
                }
            }
        }
    }

    private fun showBookAdMdm(
        type: String, showTip: Boolean = false, statusLook: Boolean = true, mdmBack: () -> Unit
    ) {
        if (statusLook && !MdmTotalUtils.getMdmAppStatus()) {
            mdmBack()
            return
        }
        if (showTip) {
            if (MdmDealAdUtils.getVlaueForInters() != null) {
                showRootBookAdBack(type, statusLook, mdmBack)
                return
            }
            val dialog = DialogInitMdmLoading(this)
            dialog.show()
            MdmDealAdUtils.loadBookIntersMdm(true, statusLook)
            lifecycleScope.launch(Dispatchers.Main) {
                delay(1600)
                if (MdmDealAdUtils.getVlaueForInters() != null) {
                    showRootBookAdBack(type, statusLook, mdmBack)
                    delay(96)
                    dialog.dismissGoMdm()
                    return@launch
                }
                if (MdmDealAdUtils.getVlaueForInters() != null) {
                    delay(1600)
                    showRootBookAdBack(type, statusLook, mdmBack)
                    delay(100)
                    dialog.dismissGoMdm()
                    return@launch
                }
                delay(1600)
                showRootBookAdBack(type, statusLook, mdmBack)
                delay(96)
                dialog.dismissGoMdm()
            }
        } else {
            showRootBookAdBack(type, statusLook, mdmBack)
        }
    }

    override fun getLayoutValue(): ActivityMdmDownloadingBinding {
        return ActivityMdmDownloadingBinding.inflate(layoutInflater)
    }
}